package com.starmark.common.base.model;

import com.google.common.base.Objects;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 基本树类型对象
 *
 * @param <T>
 */
@Data
@Slf4j
public class BaseTree<T extends BaseTree> implements Serializable, Comparable<T> {

    private static final long serialVersionUID = -8315701218786982594L;
    /**
     * 父节点代码。
     */
    private String parentId;

    public BaseTree() {
    }

    public BaseTree(String parentId, String id, Integer sequence, String name) {
        this.parentId = parentId;
        this.id = id;
        this.sequence = sequence;
        this.name = name;
    }

    /**
     * 节点代码（唯一）。
     */

    private String id;
    /**
     * 排序序号。
     */
    private Integer sequence;
    /**
     * 节点名称。
     */
    private String name;

    /**
     * 子节点列表。
     */
    private List<T> children = new ArrayList<>();

    public void add(T t) {
        this.children.add(t);
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getId() == null) {
            return false;
        }
        if (!(o instanceof BaseTree)) {
            return false;
        }
        if (!o.getClass().equals(this.getClass())) {
            return false;
        }
        return this.id.equals(((BaseTree) o).getId());
    }

    @SuppressWarnings("unchecked")
    @Override
    public int compareTo(T o2) {
        T o1 = (T) this;
        // 根据sequence排序，sequence为空或相等时则根据显示名排序
        if (o2 == null) {
            return 1;
        }
        if (o1.getSequence() == null && o1.getName() == null) {
            return -1;
        }
        if (o2.getSequence() == null && o2.getName() == null) {
            return 1;
        }

        if ((o1.getSequence() == null || o1.getSequence().equals(o2.getSequence())) && o1.getName() != null) {
            return o1.getName().compareTo(o2.getName());
        }
        return o1.getSequence().compareTo(o2.getSequence());
    }


    /**
     * 两层循环实现建树
     *
     * @param treeNodes 传入的树节点列表
     * @param root      根节点
     * @return
     */
    public static <T extends BaseTree> List<T> build(List<T> treeNodes, Object root) {

        List<T> trees = new ArrayList<>();

        for (T treeNode : treeNodes) {
            if ((root==null&&treeNode.getParentId()==null)||Objects.equal(root, treeNode.getParentId())) {
                trees.add(treeNode);
            }
            for (T it : treeNodes) {
                if (Objects.equal(it.getParentId(), treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }

    /**
     * 使用递归方法建树
     *
     * @param treeNodes 传入的树节点列表
     * @param root      根节点
     * @return
     */

    public static <T extends BaseTree> List<T> buildByRecursive(List<T> treeNodes, Object root) {
        List<T> trees = new ArrayList<>();
        for (T treeNode : treeNodes) {
            if (root.equals(treeNode.getParentId())) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public static <T extends BaseTree> T findChildren(T treeNode, List<T> treeNodes) {
        for (T it : treeNodes) {
            if (Objects.equal(treeNode.getId(), it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }
}
